package com.cuijq.eduschool.exceptionhandler;

import com.cuijq.eduschool.commontuils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname GlobalExceptionHandler
 * @Description TODO
 * @Date 2021/3/25 20:39
 * @Created by CuiJQ
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

  //指定出现什么异常执行这个方法
  @ExceptionHandler(Exception.class)
  @ResponseBody //为了返回数据
  public R error(Exception e) {
    e.printStackTrace();
    log.error(e.getMessage());
    return R.error().message("执行了全局异常处理..");
  }
  @ExceptionHandler(EduException.class)
  @ResponseBody //为了返回数据
  public R error(EduException e) {
    e.printStackTrace();
    log.error(e.getMessage());
    return R.error().message(e.getMsg()).code(e.getCode());
  }
}
